package com.dubbo.userquery;

import org.json.JSONException;
import org.json.JSONObject;

import com.dubbo.mysql.CustomImprove;
import com.dubbo.mysql.Distrmysql;
import com.dubbo.redis.JedisDao;


public class DemoServiceImpl implements DemoService{
	
      //获取内存库中的用户数，即活跃用户数	
	  public long GetActiveUserNumber()
	  {
		  JedisDao jd = new JedisDao();
		  long num = jd.getDbSize();
		  return num;
	  }
	  
	  //创建客户
	  public int CreateNewUser(String ID, String Creator, String Status, String authentype, String source, String userlevel)
	  {
		  int err_code = 0;
		  if(ID != null && ID.matches("[0-9]+") && Creator!=null )
		  {
			  err_code = Distrmysql.insert(ID, Creator, Status, authentype, source, userlevel);
			  if(err_code == 0)
			  {
				  JedisDao jd = new JedisDao();
				  String result = Distrmysql.get(ID);
				  //将新建的客户信息存入redis
				  if(result!= null)
				  {
					  jd.set(ID, result);
				  }  
			  }
		  }
		  else
		  {
			  err_code = -3;
		  }
		  
		  return 0;
	  }
	  
	 //删除客户
	public int DeleteUser(String ID)
	{
		int err_code = 0;
		boolean isSuccess = true;
		 if(ID != null && ID.matches("[0-9]+"))
		 {
			 JedisDao jd = new JedisDao();
			 try
			 {
			   jd.del(ID);
			 }
			 catch(Exception e)
			 {
				 err_code = -2;
				 isSuccess = false;
			 }
			 if(isSuccess)
			 {
				 err_code = Distrmysql.delete(ID); 
			 }
		 }
		 else
		 {
			 err_code = -3;
		 }
		 
		 return err_code;
	}
	  
	//获取客户信息的通用函数，ID为客户标识，key值为信息名称，
	static String GetUserInfo(String ID, String key)
	{
		String result;
		
		//限定ID为纯数字，剔除无效查询，防止数据注入
		if(ID != null && ID.matches("[0-9]+"))
		{	
		  JedisDao jd = new JedisDao();
		  boolean getSucess = true;
		  
		  try{
			  result = jd.get(ID);
		  }
		  catch(Exception e)
		  {
			  result = Distrmysql.get(ID);
		  }
		  
		  if(result == null)
		  {
			 result = Distrmysql.get(ID); 
			 if(result == null)
			 {
			    result="无此用户";
			    getSucess = false;
			 }
			 else
			 {
				 jd.set(ID, result);
			 }
		  }
		    
		  if(getSucess)
		  {
			  try {
			  JSONObject jsonObj = new JSONObject(result);
			  result = jsonObj.getString(key);
			
		  	} catch (JSONException e) {
			   result = "错误的返回格式";
		  	}
		  }
		}
		else
		{
			result = "错误的ID格式";
		}
		  return result;
	}
	
	//批量更新接口
    public int BatchUpdateUserStatus(String batch_task, String mdfer)
    {
        int err_code = 0;
        if(batch_task != null && mdfer != null )
        {
            String tasks[] = batch_task.split("\n");
            for (String task:tasks )
            {
                String keyvalue[] = task.split(":");
                if(keyvalue.length == 2)
                {
                    String ID = keyvalue[0];
                    String status = keyvalue[1];
                    boolean isDigit = ID.matches("[0-9]+");
                    if(isDigit)
                    {
                    	err_code = DemoServiceImpl.UpdateUserInfo(ID,"Status",status,mdfer);
                    	if(err_code != 0)
                    		break;
                    }
                    else
                    {
                        err_code = -4;
                        break;
                    }     
                }
                else
                {
                    err_code = -4;
                    break;
                }
            }
        }
        else
        {
            err_code = -3;
        }
        return err_code;
    }

	
	//更新客户信息的通用函数，ID为客户标识，key值为信息名称，value为更新的值，mdfer为更新者，
	//异步更新机制
	 static int UpdateUserInfo(final String ID, final String key, final String value, final String mdfer)
	  {
		 int err_code = 0;
		 
		//限定ID为纯数字，剔除无效查询，防止数据注入
		 if(ID != null && ID.matches("[0-9]+") && key != null && value != null && mdfer != null)
		 {
		  JedisDao jd = new JedisDao();	  
		  String origin_value = jd.get(ID);
		  
		  if(origin_value == null)
		  {
			  origin_value = Distrmysql.get(ID);	  
			  if(origin_value == null)
			  {
				  origin_value="无此用户";
				  err_code = -1;
			  }
		  }
		  //如果该用户存在
		  if(err_code == 0)
		  {  			  
				  try {
				  JSONObject json_origin = new JSONObject(origin_value);
				  JSONObject json_update = json_origin.put(key, value);
				  json_update = json_update.put("Editor", mdfer);
				  jd.set(ID, json_update.toString());
				  }
				  catch (JSONException e) {
					  err_code = -2;
				  	}	  
			  //异步调用物理库的更新操作，进一步优化需要使用线程池
			  Runnable r = new Runnable() {
		            public void run() {
		            	Distrmysql.update(ID, key, value, mdfer);
		            }
		        };
		        Thread t = new Thread(r);
		        t.start();		  
		  }
		 }
		 else
		 {
			 err_code = -3;
		 }
			  	  
		  return err_code;
	  }
	
	  public int GetTodayInsertNumber()
	  {
		  return CustomImprove.gettodayinsert();
	  }
	  
	  public int GetTodayUpdateNumber()
	  {
		  return CustomImprove.gettodayupdate();
	  }
	  
	  public String GetUserStatus(String ID)
	  {
		  String result = DemoServiceImpl.GetUserInfo(ID, "Status");
		  return result;
	  }
		
	  public int UpdateUserStatus(String ID, String status, String mdfer)
	  {
		  int result = DemoServiceImpl.UpdateUserInfo(ID, "Status",status, mdfer);
		  return result;
	  }
	  
	  public String GetUserAuthentype(String ID)
	  {
		  String result = DemoServiceImpl.GetUserInfo(ID, "authentype");
		  return result;
	  }
		
	  public int UpdateUserAuthentype(String ID, String authentype, String mdfer)
	  {
		  int result = DemoServiceImpl.UpdateUserInfo(ID, "authentype", authentype, mdfer);
		  return result;
	  }
	  
	  public String GetUserSource(String ID)
	  {
		  String result = DemoServiceImpl.GetUserInfo(ID, "source");
		  return result;
	  }
		
	  public int UpdateUserSource(String ID, String source, String mdfer)
	  {
		  int result = DemoServiceImpl.UpdateUserInfo(ID, "source", source, mdfer);
		  return result;
	  }
	  
	  public String GetUserLevel(String ID)
	  {
		  String result = DemoServiceImpl.GetUserInfo(ID, "userlevel");
		  return result;
	  }
		
	  public int UpdateUserLevel(String ID, String userlevel, String mdfer)
	  {
		  int result = DemoServiceImpl.UpdateUserInfo(ID, "userlevel", userlevel, mdfer);
		  return result;
	  }
	  
	
	  
}


